package org.an.proj.contacts.ui.contact;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import java.util.List;
import me.drakeet.materialdialog.MaterialDialog;
import org.an.ku.base.itf.OnItemClickListener;
import org.an.ku.util.ToastUtil;
import org.an.proj.contacts.R;
import org.an.proj.contacts.adapter.GroupAdapter;
import org.an.proj.contacts.db.DBManager;
import org.an.proj.contacts.db.dao.ContactInfoDao;
import org.an.proj.contacts.db.dao.GroupInfoDao;
import org.an.proj.contacts.eventbus.EventController;
import org.an.proj.contacts.model.ContactInfo;
import org.an.proj.contacts.model.GroupInfo;
import org.an.proj.contacts.ui.base.BaseActivity;
import org.an.proj.contacts.ui.contact.view.InputDialog;

/**
 * Created by an on 2016/10/28.<br/>
 * 分组管理
 */

public class GroupManagerActivity extends BaseActivity
  implements OnItemClickListener<GroupInfo>, AdapterView.OnItemClickListener {

  @Bind(R.id.lv_group) ListView mLvGroup;

  private InputDialog mInputDialog;
  private MaterialDialog mDelGroupDialog;
  private GroupAdapter mGroupAdapter;
  private GroupInfoDao mGroupInfoDao;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String url = mIntentData.getString("url");
    String url2 = mIntentData.getString("intent");
    System.out.println("url = " + url);
    System.out.println("url2 = " + url2);

    setContentView(R.layout.activity_group_manager);
    ButterKnife.bind(this);
    setTitle(R.string.str_group_mananger);
    mGroupInfoDao = DBManager.getDaoSession().getGroupInfoDao();
    mLvGroup.setOnItemClickListener(this);
    mGroupAdapter = new GroupAdapter(mContext, this);
    mLvGroup.setAdapter(mGroupAdapter);
    refresh();
  }

  private void refresh() {
    mGroupAdapter.setDatas(mGroupInfoDao.queryBuilder().list());
  }

  /**
   * 添加分组
   */
  @OnClick(R.id.btn_add) public void onAddGroup() {
    if (mInputDialog == null) {
      mInputDialog = new InputDialog(mContext, new InputDialog.OnClickSureListener() {
        @Override public void onSure(String content) {
          //  添加分组
          if (TextUtils.isEmpty(content)) {
            ToastUtil.toast(mContext, R.string.toast_groupname_null);
          }
          long count = mGroupInfoDao.queryBuilder()
            .where(GroupInfoDao.Properties.GroupName.eq(content))
            .count();
          if (count != 0) {
            ToastUtil.toast(mContext, getString(R.string.alert_groupname_exist, content));
          } else {
            mGroupInfoDao.insert(new GroupInfo(null, content));
            mInputDialog.dismiss();
            refresh();
            EventController.postRefreshContactsEvent();
          }
        }
      });
      mInputDialog.setHint(getString(R.string.hint_input_group_name));
    }
    mInputDialog.show();
  }

  @Override public void onItemClick(int position, final GroupInfo info, View v) {
    switch (v.getId()) {
      case R.id.btn_delete_group:
        if (mDelGroupDialog == null) {
          mDelGroupDialog = new MaterialDialog(mContext).setCanceledOnTouchOutside(true)
            .setTitle(R.string.title_alert)
            .setNegativeButton(R.string.action_cancel, new View.OnClickListener() {
              @Override public void onClick(View v) {
                mDelGroupDialog.dismiss();
              }
            });
        }
        mDelGroupDialog.setPositiveButton(R.string.action_sure, new View.OnClickListener() {
          @Override public void onClick(View v) {
            //  删除当前分组
            mGroupInfoDao.delete(info);
            mDelGroupDialog.dismiss();
            refresh();
            EventController.postRefreshContactsEvent();
          }
        });
        mDelGroupDialog.setMessage(getString(R.string.alert_delete_group, info.getGroupName()))
          .show();
        break;
    }
  }

  @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    GroupInfo groupInfo = mGroupAdapter.getItem(position);
    // TODO 分管管理dialog
    ContactInfoDao contactInfoDao = DBManager.getDaoSession().getContactInfoDao();
    List<ContactInfo> list = contactInfoDao.queryBuilder()
      .where(ContactInfoDao.Properties.GroupId.eq(groupInfo.getId()))
      .list();
    Log.i("fcy", "----------------- " + list);
  }
}
